package pers.cocoadel.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import pers.cocoadel.springcloud.service.PaymentService;

import javax.annotation.Resource;

/**
 * @author ZhenWuWang
 */
@RestController
public class OrderController
{
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private LoadBalancerClient loadBalancerClient;

    @Resource
    private PaymentService paymentService;

    @GetMapping("/consumer/payment/get/{id}")
    @SentinelResource(value = "consumer/payment/get/{id}")
    public String getPayment(@PathVariable("id") String id)
    {
//        ServiceInstance serviceInstance = loadBalancerClient.choose("cloud-provider-payment");
//        if (serviceInstance == null)
//        {
//            return "服务器繁忙！";
//        }
//        String url = String.format("http://%s:%s/payment/get/%s", serviceInstance.getHost(), serviceInstance.getPort(), id);
//        System.out.println("request url:" + url);
//        return restTemplate.getForObject(url, String.class);
        return paymentService.getPayment(id);
    }

    @GetMapping("/consumer/payment/testA")
    public String getTestA()
    {
        return paymentService.getTestA();
    }


}
